﻿using System;
using System.Collections.Generic;
using System.Linq;
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED!
// YOU MAY USE THIS CODE: HOWEVER THIS GRANTS NO FUTURE RIGHTS.
// see http://telnetcsharp.codeplex.com/ for further details and license information
using System.Text;
using System.Net;

namespace TelnetCSharp.Demo
{
    /// <summary>
    /// Demo on Batch process using Terminal class [Only for Windows]
    /// </summary>
    public class BatchProcessDemo
    {
        
        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Terminal">Instance of Terminal class</param>
        /// <param name="ScriptFileAddress">Address of sript file upload to telnet server.</param>
        /// <param name="FtpUri">FTP path for uploading script(Telnet and FTP Must be in same machine)</param>
        /// <param name="ExecutionPath">Execution path where script will executed on Telnet server</param>
        public BatchProcessDemo(Terminal Terminal,string ScriptFileAddress,Uri FtpUri,string ExecutionPath)
        {
            _terminal = Terminal;
            this.ScriptFileAddress = ScriptFileAddress;
            this.FtpUri = FtpUri;
            this.ExecutionPath = ExecutionPath; 
            this.Credential= new NetworkCredential() { UserName = "", Password = "" };   
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Terminal">Instance of Terminal class</param>
        /// <param name="ScriptFileAddress">Address of sript file upload to telnet server.</param>
        /// <param name="FtpUri">FTP path for uploading script(Telnet and FTP Must be in same machine)</param>
        /// <param name="ExecutionPath">Execution path where script will executed on Telnet server</param>
        /// <param name="Credential">User credential for FTP server</param>
        public BatchProcessDemo(Terminal Terminal, string ScriptFileAddress, Uri FtpUri, string ExecutionPath,ICredentials Credential)
        {
            _terminal = Terminal;
            this.ScriptFileAddress = ScriptFileAddress;
            this.FtpUri = FtpUri;
            this.ExecutionPath = ExecutionPath;
            this.Credential = Credential;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Execute the script file on Telnet server
        /// </summary>
        public void ExecuteScript()
        {
            string arguments = "";
            ExecuteScript(arguments);
        }
        /// <summary>
        /// Execute the script file on Telnet server with argument
        /// </summary>
        /// <param name="arguments">Add argument to executing script</param>
        public void ExecuteScript(string arguments)
        {
            if (_terminal == null)
            {
                return;
            }
            string filename = this.FtpUri.AbsolutePath.TrimEnd(new char[] { '/' }).Replace("/", "\\");
            WebClient wc = new WebClient();
            wc.Credentials = this.Credential;
            wc.UploadFile(this.FtpUri, this.ScriptFileAddress);
            this._terminal.SendResponse("move /y " + "%systemdrive%\\inetpub\\ftproot" + filename + " " + this.ExecutionPath, true);
            this._terminal.SendResponse(this.ExecutionPath + filename+" "+arguments, true);
        }
        #endregion

        #region Property Used
        private readonly Terminal _terminal;
        /// <summary>
        /// Address of sript file upload to telnet server.
        /// </summary>
        public string ScriptFileAddress { get; set; }
        /// <summary>
        /// FTP path for upload script(Telnet and FTP Must be in same machine)
        /// </summary>
        public Uri FtpUri { get; set; }
        /// <summary>
        /// Execution path where script will executed on Telnet server
        /// </summary>
        public string ExecutionPath { get; set; }
        /// <summary>
        /// User credential for FTP server
        /// </summary>
        public ICredentials Credential { get; set; }
        #endregion

       
    }
}
